using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Web;
using System.Web.Security;

namespace BusinessUnit
{
    //this class is used to generate tree view from the data table
    //datatable must have the following columns - value, text, parent_value
    // datacolumn count is optional if you want to show count
    public class LoadToTreeView
    {
        bool bln_ShowCount = false;

        //Allows to display count next to the node text
        public bool ShowCount { get { return this.bln_ShowCount; } set { this.bln_ShowCount = value; } }

        public string ValueReplacer { get { return "##$$##"; } }
        public string TextReplacer { get { return "$$##$$"; } }
        public string Visible { get { return "####$$"; } }

        string str_AddSuffixText = "";
        string str_AddPrefixText = "";

        public string AddPrefix { get { return this.str_AddPrefixText; } set { this.str_AddPrefixText = value; } }
        public string AddSuffix { get { return this.str_AddSuffixText; } set { this.str_AddSuffixText = value; } }

        //Loads Data to Treeview from the DataTable
        //DataTable should be hierarical and should have the columns - value, text, parent_value
        //If you want to display count next to the node text, then the datatable should also have "count" datacolumn
        public void LoadTreeView(TreeView tr_structure, DataTable dt)
        {
            this.LoadTreeView(tr_structure, dt, "", "");
        }
        protected bool ValidateDataTableforColumnExistence(DataTable dt)
        {
            bool bln_status = true;
            if (!dt.Columns.Contains("value")) bln_status = false;
            else if (!dt.Columns.Contains("text")) bln_status = false;
            else if (!dt.Columns.Contains("parent_value")) bln_status = false;
            if (this.bln_ShowCount)
            {
                if (!dt.Columns.Contains("count")) bln_status = false;
            }
            return bln_status;
        }

        public void LoadTreeView(TreeView tr_structure, DataTable dt, string str_NavigateURL, string str_SelectedId)
        {

            //validate the data column
            if (!ValidateDataTableforColumnExistence(dt))
            {
                Exception ex = new Exception("DataTable Must have DataColumn (Value,Text,Parent_Value), if ShowCount is checked, then DataColumn(count) must exist");
                throw ex;
            }

            tr_structure.Nodes.Clear();
            foreach (DataRow dr in dt.Select("(parent_value='' or parent_value is null)", "value"))
            {
                TreeNode tn = new TreeNode();
                tn.NavigateUrl = "javascript:void(0)";
                if (this.AddPrefix != "")
                {
                    tn.Text += this.AddPrefix.Replace(this.ValueReplacer, dr["value"].ToString()).Replace(this.TextReplacer, dr["text"].ToString());
                }

                if (dr["text"].ToString() == "")
                    tn.Text = dr["value"].ToString();
                else
                    tn.Text = dr["text"].ToString();

                if (this.bln_ShowCount && dr["count"].ToString() != "" && Convert.ToInt32(dr["count"].ToString()) > 0)
                    tn.Text += " (" + dr["count"].ToString() + ")";
                if (this.AddSuffix != "")
                {

                    tn.Text += this.AddSuffix.Replace(this.ValueReplacer, dr["value"].ToString()).Replace(this.TextReplacer, HttpUtility.HtmlEncode(dr["text"].ToString()).Replace("'", "\\'"));
                    if (HttpContext.Current.Session["userid"] != null & HttpContext.Current.Session["userid"].ToString() == "afadmin")
                    {
                        tn.Text = tn.Text.Replace(this.Visible, "3F");
                    }
                    else
                    {
                        if (dr["text"].ToString() == "rootNode" || dr["text"].ToString() == "CS" || dr["text"].ToString() == "ABC")
                            tn.Text = tn.Text.Replace(this.Visible, "disabled='disabled'");
                    }
                }
                tn.Value = dr["value"].ToString();
                tr_structure.Nodes.Add(tn);
                this.LoadTreeNode(tn, dr["value"].ToString(), dt, str_NavigateURL, str_SelectedId);
            }
            tr_structure.ExpandAll();
        }


        protected void LoadTreeNode(TreeNode tn, string parent_value, DataTable dt, string str_NavigateURL, string str_SelectedId)
        {
            TreeNode tn_child;
            foreach (DataRow dr_child in dt.Select("parent_value='" + parent_value + "'", "value"))
            {
                tn_child = new TreeNode();
                tn_child.ImageUrl = "Images/bu_unit.gif";
                tn_child.NavigateUrl = "javascript:void(0)";
                if (this.AddPrefix != "")
                {
                    tn_child.Text += this.AddPrefix.Replace(this.ValueReplacer, dr_child["value"].ToString()).Replace(this.TextReplacer, dr_child["text"].ToString());
                }

                if (dr_child["text"].ToString() == "")
                    tn_child.Text += "&nbsp;" + dr_child["value"].ToString();
                else
                    tn_child.Text += "&nbsp;" + dr_child["text"].ToString();

                if (this.bln_ShowCount && dr_child["count"].ToString() != "" && Convert.ToInt32(dr_child["count"].ToString()) > 0)
                    tn_child.Text += " (" + dr_child["count"].ToString() + ")";

                if (str_SelectedId == dr_child["value"].ToString())
                    tn_child.Text = "<b>" + tn_child.Text + "</b>";
                if (this.AddSuffix != "")
                {
                    tn_child.Text += this.AddSuffix.Replace(this.ValueReplacer, dr_child["value"].ToString()).Replace(this.TextReplacer, HttpUtility.HtmlEncode(dr_child["text"].ToString()).Replace("'", "\\'"));

                }

                tn_child.Value = dr_child["value"].ToString();

                if (str_NavigateURL != "")
                    tn_child.NavigateUrl = str_NavigateURL + dr_child["value"].ToString();

                tn.ChildNodes.Add(tn_child);

                //add grand child to child node
                DataRow[] dr_grant_child = dt.Select("parent_value='" + dr_child["value"].ToString() + "'", "value");
                if (dr_grant_child.Length > 0)
                {
                    this.LoadTreeNode(tn_child, dr_child["value"].ToString(), dt, str_NavigateURL, str_SelectedId);
                }
            }
        }
    }
}
